/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.aftersalesservice.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.service.CrudService;
import cn.knet.sp.ncrm.common.service.ServiceException;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.dao.PotentialAfterSalesServiceDao;
import cn.knet.sp.ncrm.modules.customer.aftersalesservice.entity.PotentialAfterSalesService;

/**
 * 成单信息Service
 * @author 冯亚鹏
 * @version 2015-09-01
 */
@Service
@Transactional(readOnly = true)
public class PotentialAfterSalesServiceService extends CrudService<PotentialAfterSalesServiceDao, PotentialAfterSalesService> {

	public PotentialAfterSalesService get(String id) {
		return super.get(id);
	}
	
	public List<PotentialAfterSalesService> findList(PotentialAfterSalesService potentialAfterSalesService) {
		if (StringUtils.isBlank(potentialAfterSalesService.getPage().getOrderBy())) {
			potentialAfterSalesService.getPage().setOrderBy("a.last_updated desc");
		}
		return super.findList(potentialAfterSalesService);
	}
	
	public Page<PotentialAfterSalesService> findPage(Page<PotentialAfterSalesService> page, PotentialAfterSalesService potentialAfterSalesService) {
		return super.findPage(page, potentialAfterSalesService);
	}
	
	@Transactional(readOnly = false)
	public void save(PotentialAfterSalesService potentialAfterSalesService) {
		Date nowDate = new Date();
		if (StringUtils.isBlank(potentialAfterSalesService.getId())) {
			potentialAfterSalesService.setDateCreated(nowDate);
		}
		potentialAfterSalesService.setLastUpdated(nowDate);
		super.save(potentialAfterSalesService);
	}
	
	@Transactional(readOnly = false)
	public void delete(PotentialAfterSalesService potentialAfterSalesService) {
		super.delete(potentialAfterSalesService);
	}

	/**
	 * 展示/取消批量修改
	 * @author zhangbin
	 * @date 2015 /09/06 13:20:58
	 * @param ids the ids
	 * @param onshow the onshow
	 */
	public void onShow(String[] ids, String onshow) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		param.put("onshow", onshow);
		param.put("lastUpdated",new java.util.Date());
		dao.batchUpdateOnShow(param);
	}
	
	/**
	 *  TODO(批量添加成单信息)
	 * @author Gehaiyang
	 * @date 2015年9月8日 下午4:45:56
	 * @param potentialAfterSalesService
	 */
	@Transactional(readOnly = false)
	public void batchSaveData(PotentialAfterSalesService potentialAfterSalesService){
		PotentialAfterSalesService tempAfterSalesService = null;
		try {
			String[] fIds = potentialAfterSalesService.getfIds();
			Date nowDate = new Date();
			for (int i = 0; i < fIds.length; i++) {
				tempAfterSalesService = new PotentialAfterSalesService();
				BeanUtils.copyProperties(tempAfterSalesService,potentialAfterSalesService);
				tempAfterSalesService.setPotentialCustomerId(fIds[i]);
				tempAfterSalesService.setIsNewRecord(false);
				tempAfterSalesService.setDateCreated(nowDate);
				tempAfterSalesService.setLastUpdated(nowDate);
				this.save(tempAfterSalesService);
			}
		} catch (Exception e) {
			throw new ServiceException("保存成单信息出现异常");
		}
	}
}